package com.hellpro.lolbox.video.model;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.video.bean.VideoManitoBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/20
 * 备注:
 */

public class VideoManitoLvAdapter extends BaseAdapter {
    private List<VideoManitoBean> mList;

    public VideoManitoLvAdapter() {

    }

    public void setData(List<VideoManitoBean> list) {
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_manito_list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //适配数据
        VideoManitoBean bean = mList.get(position);
        holder.videoManitoTvName.setText(bean.getName());
        holder.videoManitoTvMsg.setText(bean.getDescription());
        holder.videoManitoTvPlatform.setText(bean.getWebsite());
        holder.videoManitoIvShow.setImageURI(Uri.parse(bean.getImg()));
        String status = bean.getStatus();
        if ("1".equals(status)) {

            holder.videoMmanitoIvPlay.setImageResource(R.mipmap.external_live_starting_icon);
            holder.videoManitoTvPlay.setText("正在直播");
        } else {
            holder.videoMmanitoIvPlay.setImageResource(R.mipmap.external_live_unstart_icon);
            holder.videoManitoTvPlay.setTextColor(Color.GRAY);
            holder.videoManitoTvPlay.setText("未开始");
        }
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.video_manito_ivShow)
        SimpleDraweeView videoManitoIvShow;
        @BindView(R.id.video_manito_tvName)
        TextView videoManitoTvName;
        @BindView(R.id.video_manito_tvMsg)
        TextView videoManitoTvMsg;
        @BindView(R.id.video_mmanito_ivPlay)
        ImageView videoMmanitoIvPlay;
        @BindView(R.id.video_manito_tvPlay)
        TextView videoManitoTvPlay;
        @BindView(R.id.video_manito_tvPlatform)
        TextView videoManitoTvPlatform;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    /**
     * Created by qly on 2016/6/22.
     * 将图片转化为圆形
     */
    public class GlideCircleTransform extends BitmapTransformation {
        public GlideCircleTransform(Context context) {
            super(context);
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return circleCrop(pool, toTransform);
        }

        private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;

            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            // TODO this could be acquired from the pool too
            Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

            Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            }

            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);
            return result;
        }

        @Override
        public String getId() {
            return getClass().getName();
        }
    }
}
